package cn.edu.ytu.edroid.easychart;

import java.util.List;

import cn.edu.ytu.edroid.easychart.EditActivity.EventDatum;

/***/
public class ChartAction {
	private List<EventDatum> dataList;
	private String cht;

	/***/
	public ChartAction(List<EventDatum> dataList, String cht) {
		this.dataList = dataList;
		this.cht = cht;
	}

	/***/
	public String convertToUrl() {
		float maxValue = dataList.get(0).value;

		String data = "";
		String tag = "";

		String barChm = "";
		String lsChm = "";

		for (int i = 0; i < dataList.size(); i++) {
			maxValue = maxValue >= dataList.get(i).value ? maxValue : dataList
					.get(i).value;

			data += dataList.get(i).value;
			tag += dataList.get(i).tag;

			// Markers used by BarChart
			barChm += "t" + dataList.get(i).tag + ",FF0000,0," + i + ",13";

			// Markers used by SparkLine
			lsChm += "s,FF0000,0," + i + ",5|t" + dataList.get(i).tag
					+ ",FF0000,0," + i + ",13";

			if (i != dataList.size() - 1) {
				data += ",";
				tag += "|";

				barChm += "|";
				lsChm += "|";
			}
		}

		// chart API for all charts except QrCode
		String url = "http://chart.apis.google.com/chart" //
				+ "?cht=" + cht // type of chart
				+ "&chma=15,15,15,15" // Margins
				+ "&chds=0," + Float.toString(maxValue); // the Range of values

		if (cht.equals("p") || cht.equals("p3")) {// 2D or 3D PieChart

			url += "&chdlp=t" // Legend Position
					+ "&chdl=" + tag // Legend Values
					+ "&chd=t:" + data; // Data

		} else if (cht.equals("bhs")) { // Horizontal Bar Chart

			url += "&chbh=a" // Bar Width Automatic
					+ "&chxt=x,x" // Two Bottom Axes
					+ "&chxl=1:|Low|Middle|High(%)" // Labels of the second Axis
					+ "&chd=t:" + data // Data
					+ "&chm=" + barChm // Markers
					+ "&chg=10,0"; // Grid Lines

		} else if (cht.equals("bvs")) { // Vertical BarChart

			url += "&chbh=a" // Bar Width Automatic
					+ "&chxt=y,y" // Two Left Axes
					+ "&chxl=1:|Low|Middle|High(%)" // Labels of the second Axis
					+ "&chd=t:" + data // Data
					+ "&chm=" + barChm // Markers
					+ "&chg=0,10"; // Grid Lines

		} else if (cht.equals("lc")) { // LineChart

			url += "&chxt=x,x,y,y" // Four Axes
					+ "&chxl=1:||| (%)" // Labels of the second Axis
					+ "|3:|Low|Middle|High (%)" // Labels of the fourth Axis
					+ "&chls=1" // Line Style
					+ "&chd=t:" + data // Data
					+ "&chm=" + lsChm // Markers
					+ "&chg=10,10"; // Grid Lines

		} else if (cht.equals("ls")) {// SparkLine

			url += "&chls=1" // Line Style
					+ "&chm=" + lsChm // Markers
					+ "&chd=t:" + data // Data
					+ "&chg=10,10"; // Grid Lines

		} else if (cht.equals("r")) {// Radar Charts

			url += "&chls=1" // Line Style
					+ "&chxt=x,y" // Two Bottom Axes
					+ "&chxl=0:|" + tag // Labels of the first Axis
					+ "|1:|0|20%|40%|60%|80%|100%"// Labels of the second Axis
					+ "&chd=t:" + data + "," + dataList.get(0).value // Data
					+ "&chm=B,FF990034,0,0,0";// Markers

		} else if (cht.equals("qr")) {// QrCode
			// API of QrCode
			url = "http://chart.apis.google.com/chart" //
					+ "?cht=" + cht // type
					+ "&chma=15,15,15,15"// Margins
					+ "&chl=" + tag; // text of the chart

		} else if (cht.equals("gm")) {

			url += "&chxt=y" // Two Bottom Axes
					+ "&chxl=0:|slow|faster|crazy" // Labels of the first Axis
					+ "&chls=1" // Line Style
					+ "&chd=t:" + data // Data
					+ "&chl=" + tag; // Tag

		}

		return url;
	}
}
